
/**
** \file       exec_pipeline.c
**
** \brief      this function execute pipeline rule
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse, De Olivera
**             Guillaume and Nguyen Viet Dung
**
** \version    0.8
**
** \date       26 November 2010
**
*/

#include "execution.h"

static int		exec_pipe(s_pipeline	*node,
				  s_command	*tmp_cmd)
{
  int			ret = 0;

  while (tmp_cmd != NULL)
    {
      ret = exec_command(tmp_cmd);
      tmp_cmd = tmp_cmd->next;
    }
  if (!strcmp(node->no, "!"))
    {
      if (ret == 0)
	return (1);
      else
	return (0);
    }
  else
    return (ret);
}

int                     exec_pipeline(s_pipeline        *node)
{
  s_command             *tmp_cmd = NULL;

  if (node != NULL && node->cmd != NULL)
  {
    if (node->cmd->next == NULL)
    {
      if (node->no != NULL && strcmp(node->no, "!") == 0)
        return (!exec_command(node->cmd));
      else
        return (exec_command(node->cmd));
    }
    else
    {
      tmp_cmd = node->cmd;

      return (exec_pipe(node, tmp_cmd));
    }
  }
  return (1);
}
